#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <mach/platform.h>

#define DRAVER_NAME "input_drv"

struct input_dev *inpdev;
struct task_struct *input_drv_task;

static input_drv_thread(void *data)
{
	while(1) {
		input_report_key(inpdev, KEY_MEDIA, 1);
		input_sync(inpdev);
		udelay(100*1000);
		input_report_key(inpdev, KEY_MEDIA, 0);
		input_sync(inpdev);
		udelay(100*1000);
		input_report_key(inpdev, KEY_END, 1);
		input_sync(inpdev);
		udelay(100*1000);
		input_report_key(inpdev, KEY_END, 0);
		input_sync(inpdev);

		udelay(1000*1000*2);
	}
}

static int input_drv_probe(struct platform_device *pdev)
{
	int ret;

	inpdev = input_allocate_device();
	if (inpdev != NULL) {
		inpdev->name = "hook-key";
		input_set_capability(inpdev, EV_KEY, KEY_MEDIA);
		input_set_capability(inpdev, EV_KEY, KEY_END);
		ret = input_register_device(inpdev);
		if (ret != 0) {
			printk("%s:%d input_register_device error!\n", __func__, __LINE__);
			return -EBUSY;
		}
		printk("%s:%d input_register_device ok!\n", __func__, __LINE__);
	} else {
		printk("%s:%d input_allocate_device error!\n", __func__, __LINE__);
	}

	input_drv_task = kthread_run(input_drv_thread, NULL, "input_drv_thread");
}

static int __exit input_drv_remove(struct platform_device *pdev)
{
}

struct platform_device input_drv_device = {
	.name = DRAVER_NAME,
};

static struct platform_driver input_drv_driver = {
	.probe = input_drv_probe,
	.remove = __exit_p(input_drv_remove),
	.driver = {
		.name = DRAVER_NAME,
		.owner = THIS_MODULE,
	},
};

int __init input_drv_init(void)
{
	int ret;

	platform_device_register(&input_drv_device);
	platform_driver_register(&input_drv_driver);

	return 0;
}

static void __exit input_drv_exit(void)
{
	kthread_stop(input_drv_task);
}

module_init(input_drv_init);
module_exit(input_drv_exit);
MODULE_LICENSE("GPL");
